package com.mlnx.netty.base.handler;


import com.mlnx.analysis.pojo.dto.RealEcgAnalysResult;
import com.mlnx.device.pojo.dto.EcgInfoMsg;
import com.mlnx.device.session.core.DeviceSession;
import com.mlnx.device.session.core.EcgDeviceSession;
import com.mlnx.device.session.core.MpDeviceSession;
import com.mlnx.mptp.model.ecg.ECGData;
import com.mlnx.mptp.model.ecg.ECGDeviceInfo;
import com.mlnx.netty.base.message.EcgMessage;

import java.util.Date;

import cn.hutool.core.date.DateUtil;
import io.netty.channel.ChannelHandlerContext;

/**
 * Created by amanda.shan on 2018/2/12.
 */
public class PushEcgHandle extends BaseSimpleChannelInboundHandler<EcgMessage> {

    private long recordTime = System.currentTimeMillis();

    private final String pattern = "yyyy-MM-dd HH:mm:ss";

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, EcgMessage msg) throws Exception {
        DeviceSession session = (DeviceSession) sessionManager.getSession(ctx.channel());

        String deviceId = msg.getDeviceId();
        Integer patientId = session.getPatientId();

        if (session instanceof EcgDeviceSession) {

            ECGData ecgData = msg.getEcgData();

            if ((System.currentTimeMillis() - recordTime) > 30 * 1000) {
                recordTime = System.currentTimeMillis();
                log.info("设备" + msg.getDeviceId() + "病人ID：" + ((EcgDeviceSession) session).getPatientId() + " " +
                        "时间:" + DateUtil.format(new Date(msg.getPacketTime()), pattern));
            }

            // 更新设备信息session
            ECGDeviceInfo ecgDeviceInfo = msg.getEcgDeviceInfo();
            EcgDeviceSession ecgDeviceSession = (EcgDeviceSession) session;

            // 更新设备信息
            ecgDeviceSession.setECGDeviceInfo(ecgDeviceInfo);

            // 新建广播EcgInfo
            EcgInfoMsg ecgInfo = new EcgInfoMsg();
            ecgInfo.setDeviceId(deviceId);
            ecgInfo.setDeviceType(ecgDeviceSession.getDeviceType());
            ecgInfo.setPatientId(ecgDeviceSession.getPatientId());
            ecgInfo.setPacketTime(msg.getPacketTime());

            // 推送设备信息
            if (ecgDeviceInfo != null && msg.isDeviceInfoUpdate()) {
                ecgInfo.setEcgDeviceInfo(ecgDeviceInfo);
            }
            // 推送心电数据
            if (ecgData.getSuccessionData() != null || ecgData.getEncrySuccessionData() != null) {

                ecgInfo.setEcgData(ecgData);

                // 不需要分析的心电数据
                if (ecgData.getSuccessionData() != null) {

                    ecgData.setFilterData(ecgData.getSuccessionData());
                    ecgDeviceSession.setECGData(ecgData);

                }
                // 需要分析的心电数据
                else if (ecgData.getEncrySuccessionData() != null) {

                    // 初始化滤波 相当于不分析当前数据
                    if (ecgDeviceSession.getFilterTimes() > 0) {
                        ecgDeviceSession.getAnalysis().initFilter();
                        ecgDeviceSession.subFilterTimes();
                    }

                    RealEcgAnalysResult result = ecgDeviceSession.getAnalysis().realAnalysis(ecgData
                            .getEncrySuccessionData(), msg.getPacketTime(), patientId, deviceId);

                    ecgData.setEcgHeart(result.getHeart());
                    ecgData.setSuccessionData(result.getEcgData());
                    ecgData.setFilterData(result.getFilterEcgData());

                    result.setEcgData(null);

                    if (result.getHeart() != null || result.getHeartResult() != null || result.getPbNumb() !=
                            null) {

                        ecgInfo.getEcgData().setEcgHeart(result.getHeart());
                        ecgInfo.setRealEcgAnalysResult(result);
                    }

                    ecgDeviceSession.setECGData(ecgData);
                    ecgDeviceSession.setRealEcgAnalysResult(result);

                }
//                ecgDeviceSession.setLastPacketTime(new Date(msg.getPacketTime()));
                if (ecgDeviceSession.isFirstEcgPacket()) {
                    ecgDeviceSession.setFirstEcgPacket(false);

                    deviceBroadcastService.castStartEcg(ecgDeviceSession.getPatientId(), ecgDeviceSession.getDeviceId());
                }

            }

            deviceBroadcastService.castEcgInfo(ecgInfo);

        } else if (session instanceof MpDeviceSession) {
            ECGData ecgData = msg.getEcgData();

            MpDeviceSession mpDeviceSession = (MpDeviceSession) session;

            // 新建广播EcgInfo
            EcgInfoMsg ecgInfo = new EcgInfoMsg();
            ecgInfo.setDeviceId(deviceId);
            ecgInfo.setDeviceType(mpDeviceSession.getDeviceType());
            ecgInfo.setPatientId(mpDeviceSession.getPatientId());
            ecgInfo.setPacketTime(msg.getPacketTime());

            ecgInfo.setEcgData(ecgData);
            mpDeviceSession.setECGData(ecgData);

            deviceBroadcastService.castEcgInfo(ecgInfo);
        } else {
            log.error("非法sesson类型");
        }
    }
}
